package com.qf.smartplatform.controller;

import com.qf.smartplatform.dto.R;
import com.qf.smartplatform.pojo.BaseUser;
import com.qf.smartplatform.pojo.SysMenu;
import com.qf.smartplatform.pojo.SysRole;
import com.qf.smartplatform.service.MenuService;
import com.qf.smartplatform.valigroup.delete;
import com.qf.smartplatform.valigroup.insert;
import com.qf.smartplatform.valigroup.select;
import com.qf.smartplatform.valigroup.update;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;

/*** @author Tgb
 * @version V1.0
 * @Project smartplatform
 * @Package com.qf.smartplatform.controller
 * @Description:
 * @Date 2022/9/12 17:03
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    private MenuService menuService;
    @Autowired
    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }

    @PostMapping("/add")
    public R  addMenu(@Validated(insert.class) @RequestBody SysMenu sysMenu){
        menuService.addMenu(sysMenu);
        return R.OK();
    }
    @PostMapping("/selectAll")
    public R selectAll(){
        List<SysRole> roleList = ((BaseUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getRoleList();
        HashSet<SysMenu> menuSet  = new HashSet<>();
        roleList.forEach(role -> {
            List<SysMenu> menuList = role.getMenuList();
            menuList.forEach(menu -> {
                if (menu.getMenuType().equalsIgnoreCase("M")) {
                    menuSet.add(menu);
                }

            });
        });
        return R.OK(menuSet);
    }
    @PostMapping("/select")
    public R selectById( @Param("menuId")Long menuId){
        System.out.println(menuId);
       return R.OK(menuService.selectById(menuId)) ;
    }
    @PostMapping("/delete")
    public R deleteById(@Validated(delete.class) @Param("menuId")Long menuId){
        menuService.deleteById(menuId);
        return R.OK() ;
    }
    @PostMapping("/update")
    public R updateMenu(@Validated(update.class) @RequestBody SysMenu sysMenu){
        menuService.updateMenu(sysMenu);
        return R.OK();
    }

}
